package account.bean;

import java.util.ArrayList;

import javax.enterprise.context.RequestScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;

import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderError;
import org.drools.builder.KnowledgeBuilderErrors;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;

import org.drools.io.ResourceFactory;
import org.drools.logger.KnowledgeRuntimeLogger;
import org.drools.logger.KnowledgeRuntimeLoggerFactory;
import org.drools.runtime.StatefulKnowledgeSession;

import account.command.AccountDroolsCommand;
import account.command.CommandException;
import account.dao.mysql.ModelHelper;
import account.drools.AccountDroolsInfo;
import account.model.AccountModel;

@ManagedBean(name = "accountInfo")
@RequestScoped
public class AccountInfo {

	private AccountModel model = null;
	private Integer id_account = 1;
	private AccountDroolsInfo droolsInfo = null;

	public AccountModel getModel() {
		// if( model != null ) return model;
		String sql = "select id_account, name, value, period, number, percent, start_date, cost from account where id_account = "
				+ id_account;
		ArrayList<AccountModel> models = new ModelHelper<AccountModel>()
				.getModel(sql, AccountModel.class);

		AccountDroolsCommand command = new AccountDroolsCommand();
		
		droolsInfo = new AccountDroolsInfo();
		droolsInfo.accountModel = models.get(0);
		
		ArrayList< AccountDroolsInfo > droolsInfos = new ArrayList<AccountDroolsInfo>();
		droolsInfos.add(droolsInfo );
		command.accountModels = droolsInfos;
		
		try {
			command.execute();
		} catch (CommandException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		/*
		command.accountModels = models;
		try {
			command.execute();
		} catch (CommandException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		*/

		return models.get(0);
	}

	public Integer getId_account() {
		return id_account;
	}

	public void setId_account(Integer id_account) {
		this.id_account = id_account;
	}

	public AccountDroolsInfo getDroolsInfo() {
		return droolsInfo;
	}
	
	

}
